// pages/calendar/calendar.js
const api = require('../../utils/api')
const allList = require('../../utils/allList.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 0,//当日活动下部滑块位置
    cityList: [],
    nowCity: '杭州',//当前城市
    nowCityIndex: 0,//当前城市index
    nowDate: null,//当前时间
    topList: [],//置顶列表
    hideList: [],//热门列表
    monthList: [],//每月列表
    listLoding: false,//全屏loading
    monthPage: 0,//每月列表
    isDown: false,//是否到底
    isRefresher: false//是否开启下拉
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      listLoding: true
    });
    api.getHomeInfo({ "loadPage": "100", "tab": "", "nowPage": "1" }).then(res => {
      this.setData({
        topImg: res.data.data[0].columnPic
      });
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  //日历加载完成
  handleLoad() {
    this.data.calendarSelect = this.selectComponent('#calendar');
    this.getMonthFirst();
  },
  //修改日历时间
  changeDate(e) {
    if (this.data.calendarSelect)
      this.data.calendarSelect.toggleView('week');
    let date = e.detail.date;
    this.setData({
      nowDate: `${date.year}-${date.month < 10 ? '0' + date.month : date.month}-${date.day < 10 ? '0' + date.day : date.day}`,
      current: 0,
      listLoding: true,
      monthList: [],
      monthPage: 0,
      isDown: false
    })
    //获取列表
    this.getCalendarTopList();
    this.getCalendarHideList();
    this.getMonthFirst();
  },
  //滑动到底部
  scrollDown(e) {
    if (this.data.isDown) return
    this.data.monthPage += 1;
    this.setData({
      listLoding: true,
      monthPage: this.data.monthPage
    })
    this.getCalendarMonthList();
  },
  //下拉刷新
  scrollRefresher() {
    this.setData({
      isRefresher: true,
      listLoding: true,
      current: 0,
      monthList: [],
      monthPage: 0,
      isDown: false
    })
    //获取列表
    this.getCalendarTopList();
    this.getCalendarHideList();
    this.getMonthFirst();
  },
  //滑动当日活动底部滑块
  changeCurrent(e) {
    this.setData({
      current: e.detail.current
    })
  },
  //选择城市
  changeCity(e) {

    this.setData({
      nowCity: e.currentTarget.dataset.name,
      monthPage: 0,
      isDown: false,
      nowCityIndex: e.currentTarget.dataset.index
    })
    if (this.data.monthList[e.currentTarget.dataset.index].newsInfoList.length < 6) {
      this.setData({
        isDown: true
      })
    }
  },
  //点击跳转详情页
  onClickMonth(e) {
    e.currentTarget.dataset.url = '/pages/calendarDetails/calendarDetails?newsId=' + e.currentTarget.dataset.newsId
    //console.log(e);
    app.toOutLink(e);
  },
  //点击跳转更多
  onClickMore(e) {
    //console.log(e);
    app.toOutLink(e);
  },
  loading() {
    return new Promise((resolve, reject) => {
      let time = setTimeout(() => {
        this.setData({
          listLoding: false,
          isRefresher: false
        })
        resolve();
        clearTimeout(time)
      }, 500)
    })
  },
  //标签拆分
  spanPullDown(list) {
    if (!list) return
    list.forEach((i, index) => {
      list[index].Url = i.Url.split(',').slice(0, 3)
    })
    return list
  },
  //置顶活动
  getCalendarTopList() {
    api.getCalendar({
      "SubTypeName": this.data.nowCity,
      "CheckTime": this.data.nowDate,
      "NowPage": "0",
      "PageSize": "5",
      "IsHide": "1",
      "IsTop": "1",
      "Month": "0"
    }).then(res => {
      if (res.data.code != 0) return
      this.setData({
        topList: res.data.data
      })
      this.loading();
    })
  },
  //热门活动
  getCalendarHideList() {
    api.getCalendar({
      "SubTypeName": this.data.nowCity,
      "CheckTime": this.data.nowDate,
      "NowPage": 0,
      "PageSize": "9",
      "IsHide": "1",
      "IsTop": "0",
      "Month": "0"
    }).then(res => {
      if (res.data.code != 0) return
      let list = [];
      for (let i = 0; i < res.data.data.length / 3; i++) {
        list.push(res.data.data.slice(i * 3, i * 3 + 3));
      }
      this.setData({
        hideList: list
      })
      this.loading();
    })
  },
  //当月活动
  getCalendarMonthList() {
    let checkTime = this.data.nowDate.replace('-', '').substr(0, 6);
    api.getCalendar({
      "SubTypeName": this.data.nowCity,
      "CheckTime": checkTime,
      "NowPage": this.data.monthPage,
      "PageSize": "6",
      "IsHide": "0",
      "IsTop": "0",
      "Month": "1"
    }).then(res => {
      if (res.data.code != 0) return
      if (res.data.data.length == 0) {
        this.loading().then(() => {
          this.setData({
            isDown: true
          })
        });
      } else {
        this.loading().then(() => {
          this.data.monthList[this.data.nowCityIndex].newsInfoList = this.data.monthList[this.data.nowCityIndex].newsInfoList.concat(this.spanPullDown(res.data.data))
          this.setData({
            monthList: this.data.monthList
          })
        })

      }
    })
  },
  //第一次获取当月活动
  getMonthFirst() {
    let checkTime = this.data.nowDate.replace('-', '').substr(0, 6);
    api.getCalendarMonth({
      "CheckTime": checkTime
    }).then(res => {
      if (res.data.code != 0) return
      let list = res.data.data
      list.forEach((i, index) => {
        list[index].newsInfoList = this.spanPullDown(i.newsInfoList)
      })
      this.setData({
        monthList: list
      })
      this.loading()
    })
  }


})
